// logout
$$(document).on("click",".logout",function(){
    $$.ajax({
        url:url_post_touristlogout,
        method:"POST",
        data: {},
        dataType:'json',
        success: function (ajaxresult) {
            //获取数据
            if(ajaxresult.code == 1){
                mainView.router.load({
                    "url":"welcome.html"
                }); 
            }else{
                ajaxfail(ajaxresult.msg);
            }
        },
    });
}); 

// navbar_city
$$(document).on("click",".navbar_city",function(){
    $$.getJSON(url_get_city, {}, function (ajaxresult) {
        //获取city数据
        if(ajaxresult.code==1){
            mainView.router.load({
                "url":"city.html",
                "context":ajaxresult.data
            });
        }else{
            ajaxfail(ajaxresult.msg);
        }
    });
});


//-------------- "我的栏目" start-----------------------------
// navbar_selfcenter
$$(document).on("click",".navbar_selfcenter",function(){
    //检查登录状态
    checklogin();
    
    //不同状态，跳转不同页面
    if(!global_login_state){
        mainView.router.load({
            "url":"welcome.html",
        });
    }else{
        var data_selfmenu,data_selfheadpart;
        var data_selfmenu_parnter= [
            {
                link:[
                    {
                        title: '基本信息',
                        class:'selfbaseinfo'                
                    },
                    {
                        title: '我的身份',
                        class:'selfID'                          
                    },
                ]
            },
            {
                link:[
                    {
                        title: '我的私信',
                        class:'selfmsg'                             
                    },
                    {
                        title: '我的订单',
                        class:'myorder'                             
                    },
                ]
            },
            {
                link:[
                    {
                        title: '处理预定',
                        class:'orderprocess'                            
                    },
                    {
                        title: '我的点评',
                        class:'myremark'                            
                    },
                ]
            },
            {
                link:[
                    {
                        title: '账户设置',
                        class:'accountsetting'                          
                    },
                ]
            },      
        ];
        var data_selfmenu_tourist= [
            {
                link:[
                    {
                        title: '基本信息',
                        class:'selfbaseinfo'                            
                    },
                ]
            },
            {
                link:[
                    {
                        title: '我的私信',
                        class:'selfmsg'                             
                    },
                    {
                        title: '我的订单',
                        class:'myorder'                             
                    },
                ]
            },
            {
                link:[
                    {
                        title: '我的点评',
                        class:'myremark'                            
                    },
                ]
            },
            {
                link:[
                    {
                        title: '账户设置',
                        class:'accountsetting'                          
                    },
                ]
            },      
        ];
        
        console.log(global_login_role);
        if(global_login_role=='GUIDER'){
            data_selfmenu = data_selfmenu_parnter;
        }else if(global_login_role=='TOURIST'){
            data_selfmenu = data_selfmenu_tourist;
        }

        var userinfo = getuserinfo();
        //没有信息则不跳转个人中心
        if(!userinfo){
        	return;
        }
        
        data_selfheadpart={
            bgurl: 'img/scopedetail/3.jpg',
            headicon:userinfo.userIcon,
            name:userinfo.realName,
        };
        var data = {
            head:data_selfheadpart,
            menu:data_selfmenu
        };
        mainView.router.load({
            "url":"selfmenu.html",
            "context":data
        });
    }
});

// selfbaseinfo 基本信息
$$(document).on("click",".selfbaseinfo",function(){
    var userinfo = getuserinfo();
    if(userinfo){
        var data_selfmenu,data_selfheadpart;
        //年龄
        var ageselect =  userageselect(userinfo.userAge);
        //性别
        var gender = userinfo.userGender;
        if(gender){
        	 var genderselect = gender=='m'?'<select name="userGender"><option selected value="m">男</option><option value="f">女</option></select>':'<select name="userGender"><option value="m">男</option><option selected value="f">女</option></select>';
        }else{
        	 var genderselect = '<select name="userGender"><option value="m">男</option><option value="f">女</option></select>';
        }
        //居住时间
        var residetimeselect = resideselect(userinfo.resideTime);

        var data_selfheadpart={
            bgurl: 'img/scopedetail/3.jpg',
            headicon:userinfo.userIcon,
            name:'<a href="#" class="button color-white size_12 div_center changeicon" style="width:20%;">更改头像</a>',
        };
        
        var data_editform= [
            {
                title: '姓氏',
                content: '<input type="text" placeholder="" name="userFamily" value="'+ userinfo.userFamily +'"/>'
            },
            {
                title: '名字',
                content: '<input type="text" placeholder="" name="userName" value="'+ userinfo.userName +'"/>'
            },
            {
                title: '年龄',
                content: ageselect
            },
            {
                title: '性别',
                content: genderselect
            },
            {
                title: '国籍',
                content: '<input type="text" placeholder="" name="nationality" value="' + userinfo.nationality + '"/>'
            },
            {
                title: '居住城市',
                content: '<input type="text" placeholder="" name="resideCity" value="'+ userinfo.resideCity +'"/>'
            },
            {
                title: '居住时间',
                content: residetimeselect
            },
            {
                title: '自我介绍',
                content: '<textarea name="introduction">' + userinfo.introduction + '</textarea>'
            },           
        ];
        var data = {
            head:data_selfheadpart,
            editform:data_editform
        }
        mainView.router.load({
            "url":"selfbaseinfo.html",
            "context":data
        });
    }
});

// selfID 我的身份
$$(document).on("click",".selfID",function(){
    $$.getJSON(url_post_getidentity, {}, function (ajaxresult) {
        if(ajaxresult.code==1){
            console.log(ajaxresult.data);
            mainView.router.load({
                "url":"selfID.html",
                "context":ajaxresult.data
            });                     
        }else{
            ajaxfail(ajaxresult.msg);
        }
    }); 
});

// selfmsg 我的私信
$$(document).on("click",".selfmsg",function(){
    $$.ajax({
        method:'POST',
        url:url_post_mymsg,
        data: {
            page:1,// 页码
        },
        dataType:'json',
        success: function (ajaxresult) {
            if(ajaxresult.code==1){                
                mainView.router.load({
                    "url":"selfmsg.html",
                    "context":ajaxresult.data
                });
            }else{
                ajaxfail(ajaxresult.msg);
            }
        },
    });    
});

// myorder ：我的订单
$$(document).on("click",".myorder",function(){
    var data_order_completed,data_order_uncomplete;
    var flag = false;
    var error  = '';
    //asyn:false
    $$.ajax({
        url:url_get_myorder,
        data: {
            size:10,// 每页显示数量
            page:1,// 页码
            status:1// (1 有效单 0 退款单)
        },
        method:"GET",
        dataType:'json',
        async:false,
        success: function (ajaxresult) {
            if(ajaxresult.code==1){
                var data_order_completed = ajaxresult.data.data;
                flag = true;
                // var html_myorderlist = myApp.myorderlistTemplate(data_myorderlist);
                // console.log(data_myorderlist);
                // $$('#myorder #completed ul').html(html_myorderlist);
            }else{
                error = ajaxresult.msg;
                // ajaxfail(ajaxresult.msg);
            }
        }
    });
    $$.ajax({
        url:url_get_myorder,
        data: {
            size:10,// 每页显示数量
            page:1,// 页码
            status:0// (1 有效单 0 退款单)
        },
        method:"GET",
        dataType:'json',
        async:false,
        success: function (ajaxresult) {
            if(ajaxresult.code==1){
                flag = true;
                var data_order_uncomplete = ajaxresult.data.data;
                // var html_myorderlist = myApp.myorderlistTemplate(data_myorderlist);
                // $$('#myorder #uncomplete ul').html(html_myorderlist);
            }else{
                error = ajaxresult.msg;
                // ajaxfail(ajaxresult.msg);
            }
        }
    });
    if(flag){
        var data = {
            completed:data_order_completed,
            uncomplete:data_order_uncomplete,
        }
        mainView.router.load({
            "url":"myorder.html",
            "context":data
        });
    }else{
        ajaxfail(error);
    }    
});

// myremark ：我的评论
$$(document).on("click",".myremark",function(){
    var data_remark_completed,data_remark_uncomplete;
    var flag = false;
    var error  = '';
    $$.ajax({
        url:url_get_mycomments,
        data: {
            size:10,// 每页显示数量
            page:1,// 页码
            iscomment:1// (1 有效单 0 退款单)
        },
        method:"GET",
        dataType:'json',
        async:false,
        success:function (ajaxresult) {
            if(ajaxresult.code==1){
                var data_remark_completed = ajaxresult.data.data;
                flag = true;
                // var html_myremarklist = myApp.myremarklist_1Template(data_myremarklist);
                // console.log(data_myremarklist);
                // $$('#myorder #completed ul').html(html_myremarklist);
            }else{
                error = ajaxresult.msg;
            }
        }
    });
    $$.ajax({
        url:url_get_mycomments,
        data: {
            size:10,// 每页显示数量
            page:1,// 页码
            iscomment:0// (1 有效单 0 退款单)
        },
        method:"GET",
        dataType:'json',
        async:false,
        success:function (ajaxresult) {
            if(ajaxresult.code==1){
                var data_remark_uncomplete = ajaxresult.data.data;
                flag = true;
                // var html_myremarklist = myApp.myremarklist_2Template(data_myremarklist);
                // $$('#myorder #uncomplete ul').html(html_myremarklist);
            }else{
                error = ajaxresult.msg;
            }
        }
    });
    if(flag){
        var data = {
            completed:data_remark_completed,
            uncomplete:data_remark_uncomplete,
        }
        mainView.router.load({
            "url":"myorder.html",
            "context":data
        });
    }else{
        ajaxfail(error);
    }    
});

// orderprocess ：我的预定
$$(document).on("click",".orderprocess",function(){
    var data_order_completed,data_order_uncomplete;
    var flag = false;
    var error  = '';
    $$.getJSON(url_get_orderlist, {
        size:10,// 每页显示数量
        page:1,// 页码
        status:1// (1 已处理 0 未处理) 
    }, function (ajaxresult) {
        if(ajaxresult.code==1){
            var data_order_completed = ajaxresult.data.data;
            flag = true;
        }else{
            error = ajaxresult.msg;
        }
    });
    $$.getJSON(url_get_orderlist, {
        size:10,// 每页显示数量
        page:1,// 页码
        status:0// (1 已处理 0 未处理)
    }, function (ajaxresult) {
        if(ajaxresult.code==1){
            var data_order_uncomplete = ajaxresult.data.data;
            flag = true;
        }else{
            error = ajaxresult.msg;
        }
    });

    if(flag){
        var data = {
            completed:data_order_completed,
            uncomplete:data_order_uncomplete,
        }
        mainView.router.load({
            "url":"orderprocess.html",
            "context":data
        });
    }else{
        ajaxfail(error);
    }    
});

// accountsetting 账户设置
$$(document).on("click",".accountsetting",function(){
    $$.getJSON(url_get_account, {}, function (ajaxresult) {
        if(ajaxresult.code==1){
            mainView.router.load({
                "url":"accountsetting.html",
                "context":ajaxresult.data
            });                     
        }else{
            ajaxfail(ajaxresult.msg);
        }
    }); 
});


//-------------- "跳转详情页" start-----------------------------

// playitem -> playitem detail
$$(document).on("click",".playitem",function(){
    var playitem_id = $$(this).data('id');
    var playitem_type = $$(this).data('type');          
    $$.getJSON(url_get_playingmethoddetail, {id:playitem_id,type:playitem_type}, function (ajaxresult) {
        if(ajaxresult.code==1){
            console.log(ajaxresult.data);
            mainView.router.load({
                "url":"playingmethoddetail.html",
                "context":ajaxresult.data
            });                     
        }else{
            ajaxfail(ajaxresult.msg);
        }
    });     
});

// selfplayitem -> selfplayitem detail
$$(document).on("click",".selfplayitem",function(){
    var selfplayitem_id = $$(this).data('id');         
    $$.getJSON(url_get_selfplayingmethoddetail, {id:selfplayitem_id}, function (ajaxresult) {
        if(ajaxresult.code==1){
            console.log(ajaxresult.data);
            mainView.router.load({
                "url":"selfplayingmethoddetail.html",
                "context":ajaxresult.data
            });                     
        }else{
            ajaxfail(ajaxresult.msg);
        }
    });     
});

// scopeitem -> scopeitem detail
$$(document).on("click",".scopeitem",function(){
    var scopeid = $$(this).data('id');
    $$.getJSON(url_get_scopedetail, {id:scopeid}, function (ajaxresult) {
        if(ajaxresult.code==1){
            mainView.router.load({
                "url":"scopedetail.html",
                "context":ajaxresult.data
            });                     
        }else{
            ajaxfail(ajaxresult.msg);
        }
    });    
});

// orderitem -> orderitem detail
$$(document).on("click",".orderitem",function(){
    if(global_login_role=='GUIDER'){
        var orderid = $$(this).data("id");
        $$.getJSON(url_get_orderdetail, {id:orderid}, function (ajaxresult) {
            if(ajaxresult.code==1){
                console.log(ajaxresult.data);
                mainView.router.load({
                    "url":"orderdetail_guider.html",
                    "context":ajaxresult.data
                });                     
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    }else if(global_login_role=='TOURIST'){
        var orderid = $$(this).data("id");
        $$.getJSON(url_get_myorderdetail, {id:orderid}, function (ajaxresult) {
            if(ajaxresult.code==1){
                console.log(ajaxresult.data);
                mainView.router.load({
                    "url":"orderdetail.html",
                    "context":ajaxresult.data
                });                     
            }else{
                ajaxfail(ajaxresult.msg);
            }
        });
    }    
});

//-------------- 上传照片页 start-----------------------------
// uploadIDpaper 上传身份证件
$$(document).on("click",".uploadIDpaper",function(){
    var picurl = $$(this).data('pic');
    var urlstate = false
    if(picurl){
        urlstate = true
    }
    mainView.router.load({
        "url":"uploadIDpaper.html",
        "context":{
            urlstate:urlstate,
            picurl:picurl
        }
    });
});

// uploadCareerpaper 上传职业证件
$$(document).on("click",".uploadCareerpaper",function(){
    var picurl = $$(this).data('pic');
    var urlstate = false
    if(picurl){
        urlstate = true
    }
    mainView.router.load({
        "url":"uploadCareerpaper.html",
        "context":{
            urlstate:urlstate,
            picurl:picurl
        }
    });
});